package edu.chds.data
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class Model extends EventDispatcher
	{
		public static const MODEL_CHANGE:String = "modelChange";
		
		public var currentIndex:int = 0;
		public var totalItems:int;
		protected var _loader:URLLoader;
		public var data:*;
		
		public function Model()
		{
			_loader = new URLLoader;
			_loader.addEventListener(Event.COMPLETE, dataLoaded);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, ioError);
		}
		public function load(req:URLRequest):void {
			_loader.load(req);
		}
		public function setCurrentIndex(index:int):void {
			currentIndex = index;
			controlCurrentIndex();
			updateData();
			
			dispatchEvent(new Event(Model.MODEL_CHANGE));
		}
		private function controlCurrentIndex():void {
			if (currentIndex >= totalItems) {
				currentIndex = 0;
			} else if (currentIndex < 0) {
				currentIndex = totalItems - 1;
			}
		}
		protected function updateData():void {
			
		}
		protected function dataLoaded(e:Event):void {
			data = e.target.data;
			setCurrentIndex(currentIndex);
		}
		protected function ioError(e:IOErrorEvent):void {
			
		}
		
	}
}